define(['app'], function (app) {
    app.controller('borrowerMngrCtrl', ['$scope', '$rootScope', '$location', '$timeout', '$http', '$q', 'ngTableParams', 'borrowerMngrService', '$state',
       function ($scope, $rootScope, $location, $timeout, $http, $q, ngTableParams, borrowerMngrService, $state) {

           //代码书写步骤
           //1.用【$scope】定义公共类model,和页面绑定的model
           //2.用【var】定义controller内部用的变量
           //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
           //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
           //定义规则，按可用性分忧优先级，公共的在前，private的在后
           //

           $scope.goToEditPage = function () {
               var borrowerId = "1001";
               $location.path("/book/borrowerMngr/edit")
                   .search({
                       id: borrowerId
                   });
           }

           //初始化listModel
           $scope.tableListData = [];
           $scope.filter = {};
           $scope.model = {};

           $scope.getFirst = function (params) {
               if (params.total() <= (params.page() - 1) * params.count()) {
                   return Math.floor(params.total() / params.count()) * params.count() + 1;
               }
               else {
                   return (params.page() - 1) * params.count() + 1;
               }
           };

           /**
            * 查询tableList数据
            * @return {[object]} [返回table列表数据]
            */
           $scope.tableParams = new ngTableParams({ sorting: { 'id': 'desc' } }, {
               getData: function (params) {
                   // $scope.global.isBusy = true;//暂时没有
                   return borrowerMngrService.query(params, $scope.filter).then(
                       function (response) {
                           console.log(response);
                           // $scope.global.isBusy = false;
                           if (response.status == 200) {
                               $scope.first = $scope.getFirst(params);
                               return response.data.list;
                           } else {
                               console.log(response);
                               alert("查询数据失败!");
                           }
                       },
                       function (code) {
                           alert("系统出现问题了！");
                       });
               }
           });

           $scope.reflash = function () {
               $scope.tableParams.page(1);
               $scope.tableParams.reload();
           };

           $scope.edit = function (item) {
               // $scope.global.isBusy = true;//暂时没有
               if (item == null) {
                   $state.go("book.borrowerMngr.edit");
               } else {
                   $state.go("book.borrowerMngr.edit", { 'id': item.id });
               }
           };

           $scope.update = function (item) {
               // $scope.global.isBusy = true;//暂时没有
               if (!confirm("确认图书已归还吗？")) {
                   return;
               }
               borrowerMngrService.update(item).then(
                     function (response) {
                         if (response.status == 200) {
                             $scope.reflash();
                         } else {
                             console.log(response);
                             alert("更新失败!");
                         }
                     },
                     function (code) {
                         alert("系统出现问题了！");
                     });
           };


           var init = function () {
               console.log("BorrowerMngrCtrl init");
               $scope.dashboardModel.isActiveMenu = 'borrowerMngr';//左侧菜单按钮赋初值
           };

           init();
       }]);
});